package com.java.zhuiyun.api.sys_role.controller;

import com.java.zhuiyun.api.sys_role.entity.Role;
import com.java.zhuiyun.api.sys_role.service.IRoleService;
import com.java.zhuiyun.common.response.CommonResult;
import com.java.zhuiyun.util.pageUtil.PageRequest;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName RoleController
 * @Description TODO
 * @Author xy
 * @Date 2023/8/16 15:47
 **/
@RestController
@RequestMapping("/Role")
public class RoleController {
    @Resource
    IRoleService roleService;

    /**
     * @Author xy
     * @Description 全查
     * @Date 2023/8/15 18:50
     * @Return  * @param null
     */
    @GetMapping("/RoleList")
    public CommonResult RoleList(Role role, PageRequest pageRequest){
        return CommonResult.ok(roleService.RoleList(role,pageRequest));
    }

    /**
     * @Author xy
     * @Description 添加
     * @Date 2023/8/15 20:14
     * @Return  * @param null
     */
    @PostMapping("/RoleAdd")
    public CommonResult RoleAdd(@RequestBody Role role){
        int i = roleService.RoleAdd(role);
        if (i>0){
            return CommonResult.ok("恭喜您，添加成功");
        }else {
            return CommonResult.error("很抱歉，添加失败");
        }
    }

    /**
     * @Author xy
     * @Description 修改
     * @Date 2023/8/15 16:26
     * @Return  * @param null
     */
    @PostMapping("/RoleUpdate")
    public CommonResult RoleUpdate(@RequestBody Role role){
        int i = roleService.RoleUpdate(role);
        if (i>0){
            return CommonResult.ok("恭喜您，修改成功");
        }else {
            return CommonResult.error("很抱歉，修改失败");
        }
    }

    /**
     * @Author xy
     * @Description 逻辑删除
     * @Date 2023/8/15 16:41
     * @Return  * @param null
     */
    @DeleteMapping("/RoleDelete")
    public CommonResult RoleDelete(Integer id){
        int i = roleService.RoleDelete(id);
        if (i>0){
            return CommonResult.ok("恭喜您，删除成功");
        }else {
            return CommonResult.error("很抱歉，删除失败");
        }
    }

    /**
     * 绑定角色和菜单的关系
     * @param roleId 角色id
     * @param menuIds 菜单id数组
     * @return
     */
    @PostMapping("/roleMenu/{roleId}")
    public CommonResult roleMenu(@PathVariable Integer roleId, @RequestBody List<Integer> menuIds) {
        roleService.setRoleMenu(roleId, menuIds);
        return CommonResult.ok();
    }

    @GetMapping("/roleMenu/{roleId}")
    public CommonResult getRoleMenu(@PathVariable Integer roleId) {
        return CommonResult.ok( roleService.getRoleMenu(roleId));
    }
}
